<?php
/**
 * 系统信息管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-24
 */
namespace Admin\Controller;
use \Org\Com\Tool;

class SystemController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize(){       
        parent::_initialize();
        $this->db   =D("Log");
    }
    
    /*
     * 日志管理列表
     * 
     * @return #
     */
    public function index(){
        if(I("request.content")){
            $map['contemt'] =I("request.content","","trim");
        }
        //$map=$this->_search("Log");      
        if(Tool::request("add_time")){
            $add_time       =Tool::strTime(Tool::request("add_time"));
            $map['add_time']=array("egt",$add_time);
        }
        $map['state']   =1;
        $sortBy         ='add_time';
        $logList=$this->_list($this->db,$map,$sortBy,'','add_time');
        $this->assign("logList",$logList);
        $this->display();
    }
    
    /*
     * 系统信息
     * 
     * @return #
     */
    public function systemInfo(){
        $info = array(
            '操作系统' => PHP_OS,
            '运行环境' => $_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式' => php_sapi_name(),
            'ThinkPHP版本' => THINK_VERSION . ' [ <a href="http://thinkphp.cn" target="_blank">查看最新版本</a> ]',
            '上传附件限制' => ini_get('upload_max_filesize'),
            '执行时间限制' => ini_get('max_execution_time') . '秒',
            '服务器时间' => date("Y年n月j日 H:i:s"),
            '北京时间' => gmdate("Y年n月j日 H:i:s", time() + 8 * 3600),
            '服务器域名/IP' => $_SERVER['SERVER_NAME'] . ' [ ' . gethostbyname($_SERVER['SERVER_NAME']) . ' ]',
            '剩余空间' => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',
            'register_globals' => get_cfg_var("register_globals") == "1" ? "ON" : "OFF",
            'magic_quotes_gpc' => (1 === get_magic_quotes_gpc()) ? 'YES' : 'NO',
            'magic_quotes_runtime' => (1 === get_magic_quotes_runtime()) ? 'YES' : 'NO',
        );
        $this->assign('info', $info);
        $this->display();    
    }
    
    /*
     * 数据库备份
     * 
     * @return #
     */
    public function dumpDB(){
        $host = C('DB_HOST') . (C('DB_PORT') ? ":" . C('DB_PORT') : '');
        $user = C('DB_USER');
        $password = C('DB_PWD');
        $dbname = C('DB_NAME');

        //连接mysql数据库
        if (!($link = mysqli_connect($host, $user, $password))) {
            $this->error('数据库连接失败');
        }
        //是否存在该数据库
        if (!mysqli_select_db($link, $dbname)) {
            $this->error('不存在数据库:' . $dbname);
        }
        mysqli_query($link, "set names 'utf8'");
        $mysql = "set charset utf8;\r\n";
        $q1 = mysqli_query("show tables");
        while ($t = mysqli_fetch_array($q1)) {
            $table = $t [0];
            $q2 = mysqli_query($link, "show create table `$table`");
            $sql = mysqli_fetch_array($q2);
            $mysql .= $sql ['Create Table'] . ";\r\n";
            $q3 = mysqli_query($link, "select * from `$table`");
            while ($data = mysqli_fetch_assoc($q3)) {
                $keys = array_keys($data);
                $keys = array_map('addslashes', $keys);
                $keys = join('`,`', $keys);
                $keys = "`" . $keys . "`";
                $vals = array_values($data);
                $vals = array_map('addslashes', $vals);
                $vals = join("','", $vals);
                $vals = "'" . $vals . "'";
                $mysql .= "insert into `$table`($keys) values($vals);\r\n";
            }
        }

        $dir = "../Public/mysql_bak";
        $filename = $dbname . date('Ymd') . ".sql";
        $path = $dir . "/" . $filename;

//        if (!is_dir($dir)) {
//            if(!mkdir($dir)){// 尝试创建目录
//                $this->error ( '无法目录'.$dir);
//            }
//        }
        $fp = fopen($path, 'w');
        fputs($fp, $mysql);
        fclose($fp);

        $this->success('数据备份成功<br><span style="color:red">' . $filename . '</span>');
    }    
   
}